<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.yhustc.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * YBlog日志列表模块 首页或者日志分类或者归档
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */



class BlogAction extends BaseAction
{
	/**
     +----------------------------------------------------------
     * 初始化函数
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function _initialize()
	{
		parent::_initialize();
		
		if($this->options["blog_public"] != "open")
		{
			$this->assign("title",$this->options["blogname"]);
			$this->error("站点已关闭<br />".$this->options["close_reason"]);
		}

		if(isset($_GET["preview"])) //得把预览时用于切换模板的COOKIE变量注销掉
			Cookie::delete("think_template");
	}
	
	/**
     +----------------------------------------------------------
     * 用于优化SEO的空操作,根据ID或者短标题取对应的日志记录
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function _empty()
	{ //用来显示单篇日志或页面的空操作
		
		$where = array();
		$where["post_status"] = "publish"; //访问的日志或页面必须是已发布的
		if(is_numeric(ACTION_NAME))
			$where["ID"] = ACTION_NAME;
		else if(ACTION_NAME == "post")
			$where["ID"] = $_GET["ID"];
		else
			$where["post_name"] = ACTION_NAME;
		
		$this->show($where);
	}
	
	/**
     +----------------------------------------------------------
     * 根据where条件显示单篇日志
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function show($where)
	{
		$postViewDao = D("PostsView");
		$postVo = $postViewDao->where($where)->xfind();

		if(!$postVo)
			$this->error("你正试图访问一下不存在的页面");
		
		$postVo["post_content"] = str_ireplace("<p>[separator]</p>","",$postVo["post_content"]); //把用于显示摘要的分割符去掉

		//没有插入到日志中的附件，使用列表输出
		$attachList = array();
		foreach($postVo["attachList"] as $attach)
		{
			$html = D("Attach")->parseAttach($this->options,$attach);
			$attachList[] = $html;
		}
		
		//tags
		//由于MANY_TO_MANY的查询要写第三个关联表的全名或者是第三个关联表的名称是由这两个关联表用_连接组成的,posts,tags,tagged没有这个规律,而写全名表前缀就固定了,所以不用关联,自己控制
		$tagCondition = array();
		$tagCondition["postid"] = $postVo["ID"];
		$tagDao = D("Tags");
		$tagResult = $tagDao->findAll($tagCondition);
		$tags = "";
		if(count($tagResult) > 0)
		{
			foreach($tagResult as $tag)		
				$tags .= "<a href='".url(urlencode($tag["tag_name"]),"Tag")."'>".Input::forShow($tag["tag_name"])."</a>,";
		}

		if($postVo["post_type"]=="post") //只有日志才显示上/下篇的链接
		{
			$postDao = D("Posts");
			$prePostVo = $postDao->field("ID,post_title,post_name")->where("post_type='post' AND post_date<'".$postVo["post_date"]."' AND post_type='post' AND post_status='publish'")->order("post_date DESC")->find();
			$nextPostVo = $postDao->field("ID,post_title,post_name")->where("post_type='post' AND post_date>'".$postVo["post_date"]."' AND post_type='post' AND post_status='publish'")->order("post_date ASC")->find();

			$this->assign("prePostVo",$prePostVo);
			$this->assign("nextPostVo",$nextPostVo);
		}
		
		$this->assign("titlePrefix",$postVo["post_title"]." &gt;&gt; ");
		$this->assign("post",$postVo);;
		$this->assign("attachList",$attachList);
		$this->assign("tags",rtrim($tags,","));
		$this->assign("pageFile","Blog:single");		
		$this->layout();
	}

	/**
     +----------------------------------------------------------
     * 日志列表
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function index()
	{
		$postsDao = D("PostsView");
		
		$where = array();
		$where["post_type"] = "post"; //只取日志,页面不做列表,只做单独的页面显示
		$where["post_status"] = "publish"; //访问的日志或页面必须是已发布的
		if(isset($_GET["filter"]))
		{
			switch($_GET["filter"])
			{
				case "category": $where["slug"] = $_GET["v"];
					$categoryDao = D("Category");
					$category = $categoryDao->getField("name","slug='".$_GET["v"]."'");
					$this->assign("titlePrefix",$category." &gt;&gt; ");
					break;
				case "archive": $nextFirstDay = date('Y-m-01 00:00:00',strtotime('+1 month',strtotime($_GET["v"])));
					$where["post_date"] = array(array("egt",$_GET["v"]."-01 00:00:00"),array("lt",$nextFirstDay),"AND"); //本月1号开始到下个月1号结束
					$this->assign("titlePrefix",str_replace("-","年",$_GET["v"])."月日志归档 &gt;&gt; ");
					break;
				case "tag": $tagCondition = array();
					$tagCondition["tag_name"] = urldecode($_GET["v"]);
					$tagDao = D("Tags");
					$rs = $tagDao->field("postid")->where($tagCondition)->findAll();
					$postIds = "0"; //得有个空项,不然如果这个tag没有关系的日志,会出现IN条件的错误
					foreach($rs as $item)
						$postIds .= ",".$item["postid"];
					
					$where["ID"] = array("IN",$postIds);
					$this->assign("titlePrefix",urldecode($_GET["v"])." &gt;&gt; ");
					break;
				default:
					break;
			}
		}
		
		$total = $postsDao->count($where);
		import("@.ORG.Page");
		$page = new Page($total,$this->options["posts_per_page"]);
		$postList = $postsDao->where($where)->order("top DESC,post_date DESC")->limit($page->firstRow.','.$page->listRows)->findAll();
		
		//tags
		//因为每个日志都要查自己的tags,大家都查会有很多个查询,所以先一次查,然后在内存中分发
		if(count($postList) > 0)
		{
			$postIds = array();
			foreach($postList as $post)
				$postIds[] = $post["ID"];
			
			$tagCondition = array();
			$tagCondition["postid"] = array("in",implode(",",$postIds));
			$tagDao = D("Tags");
			$tagResult = $tagDao->findAll($tagCondition);
			if(count($tagResult)>0)
			{
				foreach($postList as &$post)
				{
					$tags = "";
					foreach($tagResult as $tag)
					{
						if($tag["postid"] == $post["ID"])
							$tags .= "<a href='".url(urlencode($tag["tag_name"]),"Tag")."'>".Input::forShow($tag["tag_name"])."</a>,";
					}
					$post["tags"] = rtrim($tags,",");
				}
			}
		}

		$this->assign("postList",$postList);
		$this->assign("navigation",$page->show());
		$this->assign("pageFile","Blog:index");
		$this->layout();
	}
	
	/**
     +----------------------------------------------------------
     * RSS的内容输出
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function feed()
	{
		import("@.ORG.RSS");
		$RSS = new RSS($this->options["blogname"], $this->options["siteurl"], $this->options["blogdescription"]);
		
		//根据配置取最近的日志
		
		$where = array();
		$where["post_type"] = "post"; //只取日志,页面不在RSS中显示
		$where["post_status"] = "publish"; //RSS中的日志必须是已发布的
		
		$postDao = D("Posts");
		$postList = $postDao->field("ID,post_title,post_content,post_date")->where($where)->order("top DESC,post_date DESC")->limit('0,'.$this->options["posts_per_rss"])->findAll();

		foreach($postList as $post)
		{
			$title = $post['post_title'];
			$link = str_replace('&','&amp;','http://'.$_SERVER['HTTP_HOST'].url($post['ID'],'Blog'));
			$pubDate = $post['post_date'];
			$description = "";
			
			if($this->options["rss_use_excerpt"] == "1" && stristr($post["post_content"],"<p>[separator]</p>"))
				//输出摘要
				$description = substr($post["post_content"],0,stripos($post["post_content"],"<p>[separator]</p>"));
			else
				//输出全文
				$description = str_ireplace("<p>[separator]</p>","",$post["post_content"]); //把用于显示摘要的分割符去掉

			$RSS->AddItem($title,$link,$description,$pubDate);
		}
		
		$RSS->Display();
	}
} 
?>